package xyz.xtt.equity.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.hutool.json.JSONUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Positive;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.common.response.ApiResult;
import xyz.xtt.equity.api.dto.TblUserBenefitByAssetIdDto;
import xyz.xtt.equity.api.enums.BenefitEnum;
import xyz.xtt.equity.api.enums.UserBenefitSourceTypeEnum;
import xyz.xtt.equity.client.request.SendFreeBenefitPackageRequest;
import xyz.xtt.equity.client.request.TblUserBenefitDto;
import xyz.xtt.equity.entity.TblBenefitPackage;
import xyz.xtt.equity.service.ITblBenefitService;
import xyz.xtt.equity.service.ITblUserBenefitService;

/**
 * @author dale
 * @date 2025/3/4
 **/
@Tag(name = "权益管理", description = "权益管理")
@RefreshScope
@Slf4j
@Schema(name = "权益管理", description = "权益管理")
@RequestMapping("/api/nft/equity")
@RestController
public class ExchangeBenefitController {
	@Autowired
	private ITblBenefitService benefitService;
	@Autowired
	private ITblUserBenefitService tblUserBenefitService;

	@Operation(summary = "获取用户权益", description = "获取用户权益")
	@GetMapping("/me")
	public ApiResult<Map<String, Integer>> getBenefit(@RequestHeader("uid") String uid, @RequestParam("benefitCodes") String benefitCodes) {
		return ApiResult.ok(benefitService.getUserBenefits(Long.valueOf(uid), benefitCodes));
	}

	@Operation(summary = "获取3日内VIP将要过期的用户权益记录集合", description = "获取3日内VIP将要过期的用户权益记录集合")
	@GetMapping("/expiring")
	public ApiResult<List<TblUserBenefitDto>> getExpiringUserBenefits(@RequestParam("pageNo") @Positive int pageNo,
			@RequestParam("pageSize") @Positive @Max(100) int pageSize, @RequestParam("benefitCode") String benefitCode) {
		return ApiResult.ok(benefitService.getExpiringUserBenefits(pageNo, pageSize, benefitCode));
	}

	@Operation(summary = "查询用户最新一条权益记录", description = "查询用户最新一条权益记录")
	@GetMapping("/latest")
	public ApiResult<TblUserBenefitDto> getLatestUserBenefit(@RequestHeader("uid") String uid, @RequestParam("benefitCode") String benefitCode) {
		return ApiResult.ok(tblUserBenefitService.queryLatestUserBenefitDto(Long.valueOf(uid), BenefitEnum.getByCode(benefitCode)));
	}

	@Operation(summary = "赠送免费权益包", description = "赠送免费权益包")
	@PostMapping("/sendFreeBenefitPackage")
	public ApiResult<Boolean> sendFreeBenefitPackage(@RequestBody @Valid SendFreeBenefitPackageRequest request) {
		log.info("/api/nft/equity/sendFreeBenefitPackage param:{}", JSONUtil.toJsonStr(request));
		benefitService
				.recordAndSendFreeBenefitPackage(request.getPackageCode(), request.getAppUserId(), request.getSendNum(), UserBenefitSourceTypeEnum
						.valueOf(request.getSourceType()), null);
		return ApiResult.ok(true);
	}

	@Operation(summary = "查询个人在某个资产下的权益统计", description = "查询个人在某个资产下的权益统计")
	@GetMapping("/getBenefitDetailInfo")
	public ApiResult<TblUserBenefitByAssetIdDto> getBenefitDetailInfo(@RequestHeader("uid") String uid, @RequestParam("assetId") String assetId) {
		log.info("/api/nft/equity/getBenefitDetailInfo uid:{},assetId:{}", uid, assetId);
		TblUserBenefitByAssetIdDto result = benefitService.getBenefitDetailInfo(uid, assetId);
		return ApiResult.ok(result);
	}

	@Operation(summary = "查询商家铸造时权益包信息", description = "查询铸造时权益包信息")
	@GetMapping("/businessCastingBenefit")
	public ApiResult<List<TblBenefitPackage>> getBusinessCastingBenefit() {
		log.info("/api/nft/equity/getBusinessCastingBenefit");
		List<TblBenefitPackage> tblBenefits = benefitService.getBusinessCastingBenefit();
		return ApiResult.ok(tblBenefits);
	}
}
